package eteam.aps.service.impl;

import eteam.aps.common.component.LoginUserRedisService;
import eteam.aps.common.entity.ParentEntity;
import eteam.aps.common.exceptions.BusinessException;
import eteam.aps.common.helper.Helper;
import eteam.aps.mapper.ProductProcessMapper;
import eteam.aps.mapper.ProductProcessPreserveMapper;
import eteam.aps.mapper.WorkingProcedureMapper;
import eteam.aps.model.dto.ProductPreserveDto;
import eteam.aps.model.entity.ProductProcess;
import eteam.aps.model.entity.ProductProcessPreserve;
import eteam.aps.model.vo.pruduct_process.ProductProcessVo;
import eteam.aps.model.vo.working_procedure.WorkingProVo;
import eteam.aps.service.ProductPreserveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * 工序维护 业务实现层
 *
 * @author LiuLei
 */
@Service
public class ProductPreserveServiceImpl implements ProductPreserveService {

    @Autowired
    private ProductProcessMapper productProcessMapper;

    @Autowired
    private ProductProcessPreserveMapper productProcessPreserveMapper;

    @Autowired
    private WorkingProcedureMapper workingProcedureMapper;

    @Autowired
    private LoginUserRedisService loginUserRedisService;

    @Override
    @Transactional
    public int ppInsert(String proInv) throws Exception {
        if (Helper.isEmpty(proInv)) throw new BusinessException(HttpStatus.BAD_REQUEST, "产品编号不能为空！");
        int res = 0;
        List<WorkingProVo> workLis = workingProcedureMapper.selectAll(ParentEntity.FSTATE_DELETED);
        if (workLis.isEmpty()) throw new BusinessException(HttpStatus.BAD_REQUEST, "暂无工序信息！");
        List<ProductProcess> insert = new LinkedList<>();
        String fuserid = loginUserRedisService.getUser().getFuserid();
        int index = 1;
        for (WorkingProVo work : workLis) {
            ProductProcess process = new ProductProcess(index++, work.getfCode(), work.getfName(), work.getfWorkTime(), proInv);
            Helper.setDefaultAddValue(process, fuserid);
            insert.add(process);
        }
        if (!insert.isEmpty()) res = productProcessMapper.add(insert);
        return res;
    }

    @Override
    @Transactional
    public int pppInsert(String mocInv) throws Exception {
        if (Helper.isEmpty(mocInv)) throw new BusinessException(HttpStatus.BAD_REQUEST, "制令编号不能为空！");
        int res = 0;
        List<WorkingProVo> workLis = workingProcedureMapper.selectAll(ParentEntity.FSTATE_DELETED);
        if (workLis.isEmpty()) throw new BusinessException(HttpStatus.BAD_REQUEST, "暂无工序信息！");
        List<ProductProcessPreserve> insert = new LinkedList<>();
        String fuserid = loginUserRedisService.getUser().getFuserid();
        int index = 1;
        for (WorkingProVo work : workLis) {
            ProductProcessPreserve process = new ProductProcessPreserve(index++, work.getfCode(), work.getfName(), work.getfWorkTime(), mocInv);
            Helper.setDefaultAddValue(process, fuserid);
            insert.add(process);
        }
        if (!insert.isEmpty()) res = productProcessPreserveMapper.add(insert);
        return res;
    }

    @Override
    @Transactional
    public int ppDelete(String ids) {
        if (Helper.isNotEmpty(ids))
            return productProcessMapper.delete(Arrays.asList(ids.split(",")));
        return 0;
    }

    @Override
    @Transactional
    public int pppDelete(String ids) {
        if (Helper.isNotEmpty(ids))
            return productProcessPreserveMapper.delete(Arrays.asList(ids.split(",")));
        return 0;
    }

    @Override
    @Transactional
    public int ppUpdate(ProductPreserveDto preserveDto) throws Exception {
        Helper.setEmptyWithNull(preserveDto);
        preserveDto.setModifyUserId(loginUserRedisService.getUser().getFuserid());
        preserveDto.setModifyTime(LocalDateTime.now());
        return productProcessMapper.update(preserveDto);
    }

    @Override
    @Transactional
    public int pppUpdate(ProductPreserveDto preserveDto) throws Exception {
        Helper.setEmptyWithNull(preserveDto);
        preserveDto.setModifyUserId(loginUserRedisService.getUser().getFuserid());
        preserveDto.setModifyTime(LocalDateTime.now());
        return productProcessPreserveMapper.update(preserveDto);
    }

    @Override
    @Transactional
    public List<ProductProcessVo> ppList(String proInv) {
        if (Helper.isEmpty(proInv)) throw new BusinessException(HttpStatus.BAD_REQUEST, "产品编号不能为空！");
        return productProcessMapper.list(proInv);
    }

    @Override
    @Transactional
    public List<ProductProcessVo> pppList(String mocInv) {
        if (Helper.isEmpty(mocInv)) throw new BusinessException(HttpStatus.BAD_REQUEST, "制令编号不能为空！");

        return productProcessPreserveMapper.list(mocInv);
    }
}
